package com.dpj.test;

import com.dpj.mapper.BrandMapper;
import com.dpj.mapper.UserMapper;
import com.dpj.pojo.Brand;
import com.dpj.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Demo {
    @Test
    public  void test1() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.采用代理的模式 进行sql的调用

        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);
        List<Brand> allBrand = mapper1.getAllBrand();
        Brand brand = mapper1.selectById(1);
           System.out.println(brand);
       // System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public  void test2() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.采用代理的模式 进行sql的调用

        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);
        Integer status=1;
        String companyName="华为";
        String brandName="华为";
        companyName="%"+companyName+"%";
        brandName="%"+brandName+"%";
        Brand brand = mapper1.selectByCondition(status, companyName, brandName);
        System.out.println(brand);
        // System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public  void test3() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.采用代理的模式 进行sql的调用

        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);
        Integer status=1;
        String companyName="华为";
        String brandName="华为";
        Brand brand=new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        List<Brand> brand1 = mapper1.selectByCondition3(brand);
        System.out.println(brand1);
        // System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public  void test4() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3.采用代理的模式 进行sql的调用

        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);
        Integer status=1;
        String companyName="恰恰";
        String brandName="恰恰瓜子";
        Brand brand=new Brand();
        brand.setStatus(status);
        brand.setBrandName(brandName);
        brand.setCompanyName(companyName);
        mapper1.addOne(brand);
        System.out.println(brand.getId());

        // System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public  void test5() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3.采用代理的模式 进行sql的调用

        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);
        Integer status=1;
        String description=" Im ok";

        Integer id=3;
        Brand brand=new Brand();
        brand.setId(id);
        brand.setStatus(status);
        brand.setDescription(description);

        int i = mapper1.updateBrandById(brand);
        System.out.println(i);

        // System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }


    @Test
    public  void test6() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3.采用代理的模式 进行sql的调用

        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);
        int[] ids=new int[]{3,4};

        int i = mapper1.deleteByIds(ids);
        System.out.println(i);

        // System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }

    @Test
    public  void test7() throws IOException {
        //1. 加载mybatis的核心配置文件，获取 SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //3.采用代理的模式 进行sql的调用
        Map<String,Integer> map=new Hashtable<>();
        map.put("status",1);
        BrandMapper mapper1 = sqlSession.getMapper(BrandMapper.class);

        Brand brand = mapper1.selectByMap(map);

        System.out.println(brand);
        // System.out.println(allBrand);
        //4. 释放资源
        sqlSession.close();
    }


}
